﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class ThongKeHangHoaNhapKho : UserControl
    {
        public ThongKeHangHoaNhapKho()
        {
            InitializeComponent();
        }

        private void ThongKeHangHoaNhapKho_Load(object sender, EventArgs e)
        {
            loadData();
        }

        void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            var listHangHoaNhapKho = dc.PhieuDeNghiMuaHang_HangHoas.Where(
                    x => x.HangHoaDonDatHangs.DonDatHang.PhieuNhapKhos.Single().NgayTao >= dateTimePicker1.Value.Date &&
                         x.HangHoaDonDatHangs.DonDatHang.PhieuNhapKhos.Single().NgayTao < dateTimePicker2.Value.Date.AddDays(1)
                );

            var listHangHoaNhapKhoSum = from h in listHangHoaNhapKho
                                        group h by h.HangHoa into hh
                                        select new {HangHoa = hh.Key, SoLuongNhapKho = hh.Sum(h => h.SoLuongNhapKho)};

            int count = 1;
            dgvHangHoa.Rows.Clear();
            foreach (var h in listHangHoaNhapKhoSum)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvHangHoa);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = h.HangHoa.TenHangHoa;
                r.Cells[2].Value = h.SoLuongNhapKho;
                r.Cells[3].Value = h.HangHoa.DonVi;

                dgvHangHoa.Rows.Add(r);
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            loadData();
        }
    }
}
